class PriceSegment {
  final int id;
  final int pondId;
  final double price;
  final int durationHours;
  final double? returnPrice;

  PriceSegment({
    required this.id,
    required this.pondId,
    required this.price,
    required this.durationHours,
    this.returnPrice,
  });

  factory PriceSegment.fromJson(Map<String, dynamic> json) {
    return PriceSegment(
      id: json['id'],
      pondId: json['pondId'],
      price: json['price']?.toDouble() ?? 0.0,
      durationHours: json['durationHours'] ?? 0,
      returnPrice: json['returnPrice']?.toDouble(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'pondId': pondId,
      'price': price,
      'durationHours': durationHours,
      'returnPrice': returnPrice,
    };
  }
}

class Pond {
  final int id;
  final int deptId;
  final String name;
  final String? backgroundUrl;
  final int isReturnFish; // 1: 可退鱼, 0: 不可退鱼
  final double currentWeight;
  final double actualWeight;
  final String? remark;
  final List<String> fishTypes;
  final List<PriceSegment> priceSegments;
  final String? createTime;
  final String? updateTime;

  Pond({
    required this.id,
    required this.deptId,
    required this.name,
    this.backgroundUrl,
    required this.isReturnFish,
    required this.currentWeight,
    required this.actualWeight,
    this.remark,
    required this.fishTypes,
    required this.priceSegments,
    this.createTime,
    this.updateTime,
  });

  bool get canReturnFish => isReturnFish == 1;

  factory Pond.fromJson(Map<String, dynamic> json) {
    // 处理 fishTypes 字段，可能是字符串或数组
    List<String> fishTypesList = [];
    if (json['fishTypes'] != null) {
      if (json['fishTypes'] is String) {
        // 如果是字符串，按逗号分割
        final fishTypesStr = json['fishTypes'] as String;
        if (fishTypesStr.isNotEmpty) {
          fishTypesList = fishTypesStr.split(',').map((e) => e.trim()).toList();
        }
      } else if (json['fishTypes'] is List) {
        fishTypesList = List<String>.from(json['fishTypes']);
      }
    }

    return Pond(
      id: json['id'],
      deptId: json['deptId'],
      name: json['name'] ?? '',
      backgroundUrl: json['backgroundUrl'],
      isReturnFish: json['isReturnFish'] ?? 0,
      currentWeight: json['currentWeight']?.toDouble() ?? 0.0,
      actualWeight: json['actualWeight']?.toDouble() ?? 0.0,
      remark: json['remark'],
      fishTypes: fishTypesList,
      priceSegments: (json['priceSegments'] as List?)
          ?.map((e) => PriceSegment.fromJson(e))
          .toList() ?? [],
      createTime: json['createTime'],
      updateTime: json['updateTime'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'deptId': deptId,
      'name': name,
      'backgroundUrl': backgroundUrl,
      'isReturnFish': isReturnFish,
      'currentWeight': currentWeight,
      'actualWeight': actualWeight,
      'remark': remark,
      'fishTypes': fishTypes,
      'priceSegments': priceSegments.map((e) => e.toJson()).toList(),
      'createTime': createTime,
      'updateTime': updateTime,
    };
  }
}